'''
pane
Author: Alex Foran
'''
import pygame

'''
Base class to handle displays, either full-display or overlays.
'''
class Pane:
	'''
	Constructor.
	
	Params:
	root - A reference to the current gameroot object.
	'''
	def __init__(self,root):
		self.root=root
		# start out not having an overlay. every pane can have either one or zero direct child pane in this manner.
		self.dialog=None
		# when the root pane is done, the program will exit - so let's start out not being done
		self.done=False
		# start out by showing the cursor - if the user uses the buttons it'll go away anyway
		self.cursorvisible=False
		# by default, contain no elements
		self.elements=[]
		# this must be set in order to traverse elements using keys
		self.focusedelement=None
		# holder variable to facilitate focus changes
		self.queuefocus=None
		
	'''
	Updates the data components of the pane (the non-graphic elements). This method should be extended by extending classes.
	'''
	def update(self):
		# if an item is set to receive focus, then by all means, let it have it!
		if self.queuefocus!=None:
			self.focusedelement=self.queuefocus
			#empty that queue
			self.queuefocus=None
		# update the overlay, if there is one
		if self.dialog!=None and not self.dialog.done:
			self.dialog.update()
		else:
			#clear the overlay, just in case there was one that finished
			self.dialog=None
			#update any elements belonging to this pane
			for element in self.elements:
				element.update()
			#if the mouse has been used, then let's show the cursor
			if self.root.monitor.mousemoved:
				self.cursorvisible=True
			#hide the mouse if the user is using keys
			elif self.root.monitor.lastpressedframe!=None and self.root.monitor.lastpressedframe[0:5]!="MOUSE":
				self.cursorvisible=False
	
	'''
	Draws all graphical elements of the pane. Inheriting classes' repaint methods should call this method at the end.
	''' 
	def repaint(self):
		#draw all elements
		for element in self.elements:
			element.paint()
		#draw the overlay if there is one
		if self.dialog!=None:
			self.dialog.repaint()
		else:
			#show the cursor if the user isn't using keys, or they have specified to always show it
			if self.cursorvisible or self.root.prefs["showcursor"]:
				#use the root's specified mouse cursor image
				self.root.cursor.blit(pygame.display.get_surface(),self.root.monitor.mousepos)
	
	'''
	Gives focus to the specified child element. That element will receive focus upon next update.
	
	Params:
	element - The element to give focus to.
	'''
	def givefocus(self,element):
		#just throw it in the 'queue'
		self.queuefocus=element
	
	'''
	Returns an array of all elements that have a key, just like PHP form handling.
	'''
	def getkeyedelements(self):
		#throw each element with a key into the array
		elems=[]
		for e in self.elements:
			if e.getkey()!=None:
				elems.append(e)
		#return it
		return elems

'''
Provides a slightly improved interface for drawing a cursor pointing to a given point if the image has an offset to the point.
'''
class MouseCursor:
	'''
	Constructor.
	
	Params:
	image - A pygame.image object to draw.
	offset - A tuple containing the x and y offsets for the point of the cursor.
	'''
	def __init__(self,image,offset):
		self.image=image
		self.offset=offset
	
	'''
	Draws the cursor at a given location.
	
	Params:
	surface - The pygame.surface onto which to draw the cursor
	position - A tuple containing the x and y coordinates the cursor is to point to.
	'''
	def blit(self,surface,position):
		#simple, simple math
		surface.blit(self.image,(position[0]+self.offset[0],position[1]+self.offset[1]))